import goodStorage from 'good-storage'
export class ImgUtil {
    static imgList: Record<string, string> = {}
    static async loadAllImg () {
        const imgMap =  await import.meta.glob('../assets/img/**/*.jpg')
        let absolutePath: string = ''
        let imgName: string = ''
        for (const relativePath in imgMap) {
            const res: any = await imgMap[relativePath]()
            absolutePath = res.default || ''
            if (absolutePath) {
                imgName = absolutePath.substring(absolutePath.lastIndexOf('/') + 1 )
                this.imgList[imgName] = absolutePath
            }  
        }
        console.log('loadAllImg');
        console.log(this.imgList);
        
    }

    static getImg(imgName: string) {
        return ImgUtil.imgList[imgName]
    }

    static async storageImgList() {
        this.imgList = goodStorage.get('stoageImgList') || {}
        if (Object.getOwnPropertyNames(this.imgList).length === 0) {
            await this.loadAllImg()
            console.log('storageImgList');
            console.log(this.imgList);
            
            
            goodStorage.set('stoageImgList', this.imgList)
        }
    }
}

export default ImgUtil.getImg
// function getQuotedString(str: string) {
//     const matches = str.match(/^("([^"]|\\")*")|('([^']|\\')*')/)

//     return matches ? matches[0].replace(/\\(.)/g, '$1') : ''
// }